public class BlockingQueue {
    private String[] Data = new String[100];
    private volatile int Head = 0;
    private volatile   int Tail = 0;
    private volatile   int size = 0;
    public void  put(String element) throws InterruptedException {
        synchronized (this) {
            while (size == Data.length) {
                this.wait();
            }
            Data[Tail] = element;
            Tail++;
            if (Tail==Data.length){
                Tail=0;
            }
            size++;
            this.notify();
        }
    }
    public String take() throws InterruptedException {
        synchronized (this){
            while (size==0){
                this.wait();
            }
            String result = Data[Head];
            Head++;
            if(Head==Data.length){
                Head = 0;
            }
            size--;
            this.notify();
            return  result;
        }

    }

    public static void main(String[] args) {
        BlockingQueue blockingQueue = new BlockingQueue();
        Thread thread1 = new Thread(()->{
            int num=1;
            while (true){
                try {
                    blockingQueue.put(num+"");
                    System.out.println("生产元素"+num);
                    num++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        });
        Thread thread2 = new Thread(()->{
            while (true){
                try {
                    String result= blockingQueue.take();
                    System.out.println("消费元素"+result);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        });
        thread1.start();
        thread2.start();
    }


}
